import javax.servlet.ServletException;
import javax.servlet.annotation.MultipartConfig;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.Part;
import java.io.IOException;

@MultipartConfig
@WebServlet("/upload")
public class UploadServlet extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.setContentType("text/html;charset=utf-8");
        //1.从req对象中读取Part对象
        Part part = req.getPart("MyImage");
        //2.读取Part对象中的一些参数
        System.out.println(part.getSubmittedFileName());//上传文件的真实文件名
        System.out.println(part.getContentType());//文件类型
        System.out.println(part.getSize());//文件大小
        //3.把文件列入到指定目录中
        part.write("C:/files/MyImage.jpg");
        //4.返回一个响应
        resp.getWriter().write("upload ok");
    }
}
